package com.example.demo.Controller;

import com.example.demo.Entity.Question;
import com.example.demo.Entity.SysUser;
import com.example.demo.Service.QuestionService;
import com.example.demo.Vo.AForm;

import com.example.demo.Vo.QuestionAndScore;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
public class IndexController {



    @Autowired
    private QuestionService questionService;
    @GetMapping(value = "/index")
    public String index(Model model){
        Subject subject= SecurityUtils.getSubject();
        SysUser user=(SysUser) subject.getPrincipal();
        model.addAttribute("username",user.getUsername());
        model.addAttribute("AForm",new AForm());
        return "index";
    }

    @PostMapping(value="/index")
    public String indexHandler(@ModelAttribute AForm aForm,Model model){
        String question=aForm.getQuestion();
        QuestionAndScore qas=questionService.get_most_sim(question);
        String res_answer=qas.getQuestion().getAnswer();
        String res_question=qas.getQuestion().getQuestion();
        List<Question> simlist=qas.getSimlist();
        Subject subject= SecurityUtils.getSubject();

        SysUser user=(SysUser) subject.getPrincipal();
        model.addAttribute("username",user.getUsername());
        model.addAttribute("result_question",res_question);
        model.addAttribute("result_answer",res_answer);
        model.addAttribute("simlist",simlist);

        return "index";
    }
}
